package com.zc.mall.service.impl;

import com.zc.mall.dao.CategoryMapper;
import com.zc.mall.pojo.Category;
import com.zc.mall.service.ICategoryService;
import com.zc.mall.vo.CategoryVo;
import com.zc.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.zc.mall.consts.MallConst.ROOT_PARENT_ID;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName CategoryServiceImpl.java
 * @Description 类目实现类
 * @createTime 2021年06月06日 10:34:00
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询所有类目
     *
     * @return
     */
    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
//        List<CategoryVo> categoryVoList = new ArrayList<>();
        //先把所有的数据查询出来，然后再做操作
        List<Category> categories = categoryMapper.selectAll();

        //查出parent_id=0的父目录
//        for (Category category: categories) {
//            if (category.getParentId().equals(ROOT_PARENT_ID)) {
//                CategoryVo categoryVo = new CategoryVo();
//                BeanUtils.copyProperties(category,categoryVo);
//                categoryVoList.add(categoryVo);
//            }
//        }

        //lambd+steam
        List<CategoryVo> categoryVoList = categories.stream()
                .filter(e -> e.getParentId().equals(ROOT_PARENT_ID))
                .map(this::category2CategoryVo)
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());



        //查询子目录
        findSubCategory(categoryVoList,categories);

        return ResponseVo.success(categoryVoList);
    }

    @Override
    public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
        //先把所有的数据查询出来，然后再做操作
        List<Category> categories = categoryMapper.selectAll();
        findSubCategoryId(id,resultSet,categories);
    }

    //重载方法，递归调用时就不用重复查询数据库
    private void findSubCategoryId(Integer id, Set<Integer> resultSet,List<Category> categories){
        //遍历所有数据
        for (Category category : categories) {
            //若当前的一条数据对的parentId和传入的Id相等，则该数据为传入Id数据的子类目
            if (category.getParentId().equals(id)) {
                //将当前数据的Id添加到set列表里
                resultSet.add(category.getId());
                //递归调用，此时入参id变为当前数据的Id
                findSubCategoryId(category.getId(),resultSet,categories);
            }
        }
    }


    private void findSubCategory(List<CategoryVo> categoryVoList,List<Category> categories) {

        //遍历查询当前目录，
        for (CategoryVo categoryVo: categoryVoList) {
            List<CategoryVo> subCategoryVoList = new ArrayList<>();
            //从全部数据中找到目录的子目录
            for (Category category: categories) {
                //若当前目录的Id与全部数据遍历的一个元素的parentId一致，那么该元素就为该条数据的子目录
                if (categoryVo.getId().equals(category.getParentId())) {

                    CategoryVo subCategoryVo = new CategoryVo();
                    BeanUtils.copyProperties(category,subCategoryVo);
                    subCategoryVoList.add(subCategoryVo);
                }

                //将查询出来的子目录，设置为当前的目录的子目录
                subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
                categoryVo.setSubCategories(subCategoryVoList);

                //递归调用,直至类目为null
                findSubCategory(subCategoryVoList,categories);
            }
        }
    }


    private CategoryVo category2CategoryVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category, categoryVo);
        return categoryVo;
    }


}
